package aiproject3.util;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import sequences.CharSequence;

/**
 * Generates files of characters for testing various AI frameworks.
 * 
 * @author John Schaeffer
 *
 */
public class FileGenerator {
    
    // Output filename.
    private String outName;
    
    // Tye type of file to generate.
    private CharSequence seq;
    
    /**
     * Constructs a new FileGenerator.
     * 
     * @param outName The output name of the file.
     * @param seq The character sequence to be generated.
     */
    public FileGenerator(String outName, CharSequence seq) {
        this.outName = outName;
        this.seq = seq;
    }
    
    /**
     * Sets the output filename.
     */
    public void setOutName(String outName) {
        this.outName = outName;
    }
    
    /**
     * Sets the character sequence to be used.
     */
    public void setSequence(CharSequence seq) {
        this.seq = seq;
    }
    
    /**
     * Generates a file using the generator's character sequence.
     * 
     * @param length The length of the sequence to generate.
     */
    public void genFile(int length) {
        
        System.out.println("Generating file of length " + length + " at " + outName);
        File file = new File(outName);
        System.out.println("Current dir: " + System.getProperty("user.dir"));
        if (!file.exists() && length > 0) {
            try {
                char tmp;
                FileWriter fw = new FileWriter(file);
                tmp = seq.nextChar();
                System.out.print(tmp);
                fw.write(tmp);
                for (int i = 1; i < length; i++) {
                    tmp = seq.nextChar();
                    fw.write(',');
                    fw.write(tmp);     
                    System.out.print("," + tmp);
                }
                System.out.println();
                fw.close();
            } catch (IOException ex) {
                System.out.println("Error: Unable to create new test file... \n" + ex.getMessage());
            }
            
        }
    }
}
